###############################################
#!file Makefile
#!author TooOld2Rock'nRoll (toooldtorock-n-roll@riseup.net)
#!date 2022
#!remark Last modified in 2023/oct
#!brief Makefile for the Arcade Fighter Demo.
#!see - Mecklenburg, R.  - 2009 - Managing Projects with GNU Make (Nutshell Handbooks)
###############################################
SHELL := /bin/bash # Use bash syntax

#executable name will only be set if not yet thru the command line
name ?= arcade_fighter_demo

#the project structure
srcdir	:= src
incdir	:= inc
objdir	:= .obj
create_dirs := $(objdir)
#local library
afdir := lib/ArcadeFighter
aflib := -lArcadeFighter

#flags for the compiler
override CFLAGS += -g -Wall -pthread -MP -MD
override CXXFLAGS += $(CFLAGS)
#where to look for header files
CPPFLAGS := -I$(incdir) -I$(afdir)/inc
#where to look for pre compiled libraries
LDFLAGS	:= -L$(afdir)
#list of libraries to  link, -ldl should be the last one...
LDLIBS	:= $(aflib) -lfmt -lSDL2 -lSDL2_mixer -lGL -ldl

#********************************

#list all source files
vpath %.c $(srcdir)
vpath %.cpp $(srcdir)
c_files   := $(wildcard $(srcdir)/*.c)
cpp_files := $(wildcard $(srcdir)/*.cpp)

#change path of .o files to the objdir target.
c_objs := $(patsubst $(srcdir)/%.c,$(objdir)/%.o,$(c_files))
cpp_objs := $(patsubst $(srcdir)/%.cpp,$(objdir)/%.o,$(cpp_files))

#create necessary directories if not created yet
create-output-directories := \
	$(shell for d in $(create_dirs); \
		do [[ -d $$d ]] || mkdir -p $$d; \
	done)

#if you need to debug something....
#$(info SRC_LIST: $(c_files) $(cpp_files))
#$(info OBJ_LIST: $(c_objs) $(cpp_objs))

#*******************************
#********build targets**********
#*******************************
.PHONY: all mem_profiling clean $(afdir)

# help - The default goal
#TODO make a proper help!!!
#.DEFAULT:
	#@$(MAKE) --print-data-base --question no-such-target /
	#$(GREP) -v -e '^no-such-target' -e '^makefile' /
	#$(AWK) '/^[^.%][-A-Za-z0-9_]*: { print substr($$1, 1, length($$1)-1) }' /
	#$(SORT) /
	#$(PR) --omit-pagination --width=80 --columns=4


all: $(name)

#change flags depending on target!
mem_profiling: RULE_FORWARD := mem_profiling
mem_profiling: CFLAGS += -fsanitize=address -fno-omit-frame-pointer
#mem_profiling: LDLIBS +=
mem_profiling: all

$(name): $(afdir) $(c_objs) $(cpp_objs)
	$(LINK.cpp) -o $@ $(c_objs) $(cpp_objs) $(LDLIBS)

#make .o into .d files and set as dependency (to recompile on header files changes)
#clean does not need to generate a list of dependencies!
ifneq "$(MAKECMDGOALS)" "clean"
  -include $(c_objs:.o=.d) $(cpp_objs:.o=.d)
endif


#compile .c files
$(c_objs): $(create_dirs)
	$(COMPILE.c) $(OUTPUT_OPTION) $(patsubst $(objdir)/%.o,$(srcdir)/%.c,$@)

#compile .cpp files
$(cpp_objs): $(create_dirs)
	$(COMPILE.cc) $(OUTPUT_OPTION) $(patsubst $(objdir)/%.o,$(srcdir)/%.cpp,$@)

#compile the ArcadeFighter lib
$(afdir):
	$(MAKE) --directory=$(afdir) $(MAKEFLAGS) $(RULE_FORWARD)

#remove all files generated by make
clean:
	$(MAKE) --directory=$(afdir) clean
	rm -rf $(objdir)/*
	rm -f $(name)

